const app = getApp();
function toDecimal(x) {
  var val = Number(x)
  if (!isNaN(parseFloat(val))) {
    val = val.toFixed(2);
  }
  return val;
}  
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['全部', '全部', '全部'],
    customItem: '全部',
    sex:['男','女'],
    isSex:false,
    ageArr: [],
    isAge: false,
    age: 22,
    checkbox:false,
    isSignle: [],
    isType2: [],
    isType4: [],
    isType6: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.showModal({
      title: '请注意',
      content: '请认真填写个人资料，一旦填写后将无法修改',
      showCancel: false,
      confirmText:'知道了'
    });
    that.setData({
      uid: options.uid,
      rank_img: options.rank_img,
      rank: options.rank,
      is_bee: options.is_bee
    })
     var ageArr = [];
     for (var ii = 0; ii <= 100; ii++) {
       if (ii == 0) {
         ageArr.push('无')
       } else {
         ageArr.push(ii)
       }
     };

     wx.request({
       url: app.globalData.phpUrl + 'user/api',
       data: { api_name: 'question_detail', openid: app.globalData.userData.openOId, questionnaire_id:Number(options.id)},
       method:'POST',
       success:function(d){
         that.setData({
           grade_name: d.data.data.grade_name
         });
         var isSignle = [];
         var singleIndex = 0;

         var isType2 = [];
         var isType2Index = 0;

         var isType4 = [];
         var isType4Index = 0;

         var isType6 = [];
         var isType6Index = 0;

         if(d.data.code == -1){
           wx.showToast({
             title: d.data.msg+'!',
             icon: 'none',
             duration: 2000,
           });
           return
         }
         
         for (var tt = 0; tt < d.data.data.questions.length; tt++){
           if (d.data.data.questions[tt].type == 1){
             isSignle[singleIndex] = {};
             isSignle[singleIndex].tag_name_zh = d.data.data.questions[tt].tag_name_zh;
             isSignle[singleIndex].tag_name = d.data.data.questions[tt].tag_name;
             isSignle[singleIndex].manager_type = d.data.data.questions[tt].manager_type;
             isSignle[singleIndex].id = d.data.data.questions[tt].id;
             isSignle[singleIndex].value = null;
             isSignle[singleIndex].range = [];
             isSignle[singleIndex].rangeValue = [];
             isSignle[singleIndex].index = tt
             for (var yy = 0; yy < d.data.data.questions[tt].option.length; yy++) {
               isSignle[singleIndex].range.push(d.data.data.questions[tt].option[yy].content);
               isSignle[singleIndex].rangeValue.push(d.data.data.questions[tt].option[yy].tag_option);
             }
             singleIndex++
           };

           if (d.data.data.questions[tt].type == 2) {
             isType2[isType2Index] = {};
             isType2[isType2Index].tag_name_zh = d.data.data.questions[tt].tag_name_zh;
             isType2[isType2Index].tag_name = d.data.data.questions[tt].tag_name;
             isType2[isType2Index].manager_type = d.data.data.questions[tt].manager_type;
             isType2[isType2Index].id = d.data.data.questions[tt].id;
             isType2[isType2Index].value = null;
             isType2[isType2Index].range = [];
             isType2[isType2Index].index = tt

             for (var yy = 0; yy < d.data.data.questions[tt].option.length; yy++) {
               isType2[isType2Index].range.push({content:d.data.data.questions[tt].option[yy].content,id:yy + 1,checked:''});
             }
             isType2Index++
           };
           
           if (d.data.data.questions[tt].type == 6) {
             isType6[isType6Index] = {};
             isType6[isType6Index].tag_name_zh = d.data.data.questions[tt].tag_name_zh;
             isType6[isType6Index].tag_name = d.data.data.questions[tt].tag_name;
             isType6[isType6Index].manager_type = d.data.data.questions[tt].manager_type;
             isType6[isType6Index].id = d.data.data.questions[tt].id;
             isType6[isType6Index].value = 0;
             isType6[isType6Index].range = ageArr;
             isType6[isType6Index].index = tt;
             isType6Index++
           };


           if (d.data.data.questions[tt].type == 4) {
             isType4[isType4Index] = {};
             isType4[isType4Index].tag_name_zh = d.data.data.questions[tt].tag_name_zh;
             isType4[isType4Index].tag_name = d.data.data.questions[tt].tag_name;
             isType4[isType4Index].manager_type = d.data.data.questions[tt].manager_type;
             isType4[isType4Index].id = d.data.data.questions[tt].id;
             isType4[isType4Index].value = ['全部', '全部', '全部'];
             isType4[isType4Index].range = ['全部', '全部', '全部'];
             isType4[isType4Index].index = tt;
             isType4Index++
           };
         }

         that.setData({
           questions: d.data.data.questions,
           isSignle: isSignle,
           isType2: isType2,
           isType4: isType4,
           isType6: isType6
         });
         
       }
     });
  },

  //2年龄段选择
  bindAgeChange: function (e) {
    var index = e.currentTarget.dataset.index;
    var that = this;
    that.setData({
      ['isType6[' + index + '].value']: e.detail.value
    })
  },

  bindRegionChange: function (e) {
    var index = e.currentTarget.dataset.index;
    var that = this;
    that.setData({
      ['isType4[' + index + '].range']: e.detail.value,
    });
  },
  

  //提交
  success:function(){
    var that = this;
    var newTags = [];

    for (var tt = 0; tt < that.data.isSignle.length; tt++) {
      if (that.data.isSignle[tt].value == null) {
        wx.showToast({
          title: '请填写完整' + that.data.isSignle[tt].tag_name_zh,
          icon: 'none'
        });
        return
      } else {
        newTags.push({ 'type': that.data.isSignle[tt].manager_type, 'name': that.data.isSignle[tt].tag_name[0], 'value': that.data.isSignle[tt].rangeValue[that.data.isSignle[tt].value], 'question_id': that.data.isSignle[tt].id, })
      }
    }

    for (var tt = 0; tt < that.data.isType2.length; tt++) {
      if (that.data.isType2[tt].value == 0 || that.data.isType2[tt].value == '') {
        wx.showToast({
          title: '请填写完整' + that.data.isType2[tt].tag_name_zh,
          icon: 'none'
        });
        return
      } else {
        newTags.push({ 'type': that.data.isType2[tt].manager_type, 'name': that.data.isType2[tt].tag_name[0], 'value': that.data.isType2[tt].value, 'question_id': that.data.isType2[tt].id, })
      }
    }

    for (var tt = 0; tt < that.data.isType6.length; tt++) {
      if (that.data.isType6[tt].value == 0 || that.data.isType6[tt].value == '') {
        wx.showToast({
          title: '请填写完整' + that.data.isType6[tt].tag_name_zh,
          icon: 'none'
        });
        return
      } else {
        newTags.push({ 'type': that.data.isType6[tt].manager_type, 'name': that.data.isType6[tt].tag_name[0], 'value': that.data.isType6[tt].range[that.data.isType6[tt].value], 'question_id': that.data.isType6[tt].id, })
      }
    }

    for (var tt = 0; tt < that.data.isType4.length; tt++) {
      if (that.data.isType4[tt].range[0] == '全部') {
        wx.showToast({
          title: '请填写完整' + that.data.isType4[tt].tag_name_zh,
          icon: 'none'
        });
        return
      } else {
        newTags.push({ 'type': that.data.isType4[tt].manager_type, 'name': that.data.isType4[tt].tag_name[0], 'value': that.data.isType4[tt].range[0], 'question_id': that.data.isType4[tt].id, });
        if (that.data.isType4[tt].range[1] != '全部') {
          newTags.push({ 'type': that.data.isType4[tt].manager_type, 'name': that.data.isType4[tt].tag_name[1], 'value': that.data.isType4[tt].range[1], 'question_id': that.data.isType4[tt].id, })
          if (that.data.isType4[tt].range[2] != '全部') {
            newTags.push({ 'type': that.data.isType4[tt].manager_type, 'name': that.data.isType4[tt].tag_name[2], 'value': that.data.isType4[tt].range[2], 'question_id': that.data.isType4[tt].id, })
          }
        }
      }
    }

    wx.showModal({
      title: '请注意',
      content: '你填写的资料信息是否属实',
      confirmText: '对',
      cancelText: '再检查',
      success:function(res){
        if (res.confirm){
          wx.request({
            url: app.globalData.phpUrl + 'user/api',
            data: { api_name: 'edit_info', openid: app.globalData.userData.openOId, tags: newTags },
            method: 'POST',
            success: function (res) {
              if (res.data.code == 1) {
                wx.navigateTo({
                  url: 'finish/finish?level=' + res.data.data.grade_name + '&uid=' + that.data.uid + '&rank_img=' + that.data.rank_img + '&rank=' + that.data.rank + '&is_bee=' + that.data.is_bee,
                })
              };
              if (res.data.code == -2) {
                wx.showToast({
                  image: "../../../images/attend.png",
                  title: res.data.msg,
                  duration: 2000
                })
              }
            }
          })


        } else if(res.cancel){
          return 
        }
      }
    });
  },

  changAge:function(e){
    this.setData({
      ['isSignle[' + e.currentTarget.dataset.indexs +'].value']: e.detail.value,
    });
  },

  checkboxChange: function (e) {
    var o;
    var oo;
    var that = this;

    var checkObj = that.data.checkObj;
    var isType2 = that.data.isType2;
    var value = e.detail.value;

    value = value.sort(function (x, y) {
      return x - y;
    });

    isType2[checkObj.indexs].value = [];


    for (o = 0; o < isType2[checkObj.indexs].range.length; o++) {
      isType2[checkObj.indexs].range[o].checked = ''
    };

    that.setData({
      isType2: isType2,
    });


    for (oo = 0; oo < value.length; oo++) {
      isType2[checkObj.indexs].range[Number(value[oo]) - 1].checked = true;
      isType2[checkObj.indexs].value.push(isType2[checkObj.indexs].range[Number(value[oo]) - 1].content);
    };

    if (isType2[checkObj.indexs].value.length > 0) {
      isType2[checkObj.indexs].value = isType2[checkObj.indexs].value.toString()
    }else{
      isType2[checkObj.indexs].value = null
    };

    that.setData({
      isType2: isType2
    });
  },

  hideCheckbox: function (e) {
    var that = this;
    var checkObj = {};
    checkObj.indexs = e.currentTarget.dataset.indexs;
    var checkbox = that.data.checkbox;
    that.setData({
      checkbox: checkbox == false ? true : false,
      checkObj: checkObj
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  }
})